Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: add record! macro #2926

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marcusirgens
Copy link

Add a macro that provides a more consise way of recording a new value for a field.

Motivation

The ability to record updated values for a span is very useful. However, it can be a bit verbose, and for what I assume is the most common scenario, adding fields to the current span, the update can quickly capture four lines in an already-busy function.

Here's an example from a code base I'm contributing to, with some names changed, using a helper function to format a time::Instant value inside of a tracing::Span::record call:

impl RetroEncabulator for AcmeEncabulator {
    #[tracing::instrument(fields(request.start = tracing::field::Empty, request.deadline = tracing::field::Empty))]
    async fn prevent_sidefumbling(&self, foo: Bar, baz: Qux) {
        /* omitted for brevity */

        tracing::Span::current().record(
            "request.start",
            tracing::field::display(display_instant(start)),
        );
        tracing::Span::current().record(
            "request.deadline",
            tracing::field::display(display_instant(deadline)),
        );

        /* ... etc */
    }
}

Solution

My proposal is to add a way to record a field on the current span. This PR has one such implementation, which, using the code sample above, gives:

impl RetroEncabulator for AcmeEncabulator {
    #[tracing::instrument(fields(request.start = tracing::field::Empty, request.deadline = tracing::field::Empty))]
    async fn prevent_sidefumbling(&self, foo: Bar, baz: Qux) {
        /* omitted for brevity */

        tracing::record!("request.start", %display_instant(start));
        tracing::record!("request.deadline", %display_instant(deadline));

        /* ... etc */
    }
}

I'm open to other suggestions, of course, and if the maintainers agree that this is something that should be solved in the tracing package, I need some help figuring out where such an implementation belongs. This example implementation "feels" right as tracing::field::record!, but I've yet to fully understand how macro exports work.

Add a macro that provides a more consise way of recording a new value
for a field.

Example usage:

```rust
fn my_function() {
    let my_new_value = my_other_function();

    // Using the current API:
    tracing::Span::current().record("example", tracing::field::debug(my_new_value));

    // Using this proposal:
    tracing::record!("example", ?my_new_value);
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant